package net
{
	import flash3.net.ByteArrayPlus;
	import flash3.net.MsgPacket;
	
	import global.Define;
	
	import model.*;
	
	import vo.*;

	public class GameGroup extends MessageGroup
	{
		public static const  MDM_GF_GAME:uint					=	300								//游戏消息
			
		public static const  SUB_C_LAND_SCORE:uint			=1									//用户叫分
		public static const  SUB_C_OUT_CART:uint				=2									//用户出牌
		public static const  SUB_C_PASS_CARD:uint				=3									//放弃出牌
		public static const  SUB_C_TRUSTEE:uint				=4									//托管消息
		public static const  SUB_C_BRIGHT_START:uint			=5									//明牌开始
		public static const  SUB_C_BRIGHT:uint				=6									//玩家明牌
		public static const  SUB_C_DOUBLE_SCORE:uint			=7									//加倍命令
			
			
		public static const  SUB_S_SEND_CARD:uint			=		100									//发牌命令
		public static const  SUB_S_LAND_SCORE:uint			=	101									//叫分命令
		public static const  SUB_S_GAME_START:uint			=	102									//游戏开始
		public static const  SUB_S_OUT_CARD:uint			=		103									//用户出牌
		public static const  SUB_S_PASS_CARD:uint			=		104									//放弃出牌
		public static const  SUB_S_GAME_END:uint			=		105									//游戏结束
		public static const  SUB_S_BRIGHT_START:uint		=		106									//明牌开始
		public static const  SUB_S_BRIGHT_CARD	:uint	=		107									//玩家明牌
		public static const  SUB_S_DOUBLE_SCORE:uint		=		108									//分数加倍
		public static const  SUB_S_USER_DOUBLE:uint		=		109									//用户加倍
		public static const  SUB_S_MULTIPLE	:uint		=		200									//倍数通知
		public static const  SUB_S_FLEE_LOSS	:uint		=		201									//逃跑损失
			
		public function GameGroup()
		{
			super();
		}
		
		override public function defMessage():void
		{
			addMessage(new MessageDef(MDM_GF_GAME, SUB_C_LAND_SCORE, "用户叫分", onLandScoreRequest));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_C_OUT_CART, "用户出牌", onOutCardRequest));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_C_PASS_CARD, "放弃出牌", onPassCardRequest));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_C_TRUSTEE, "托管消息", onTrusteeRequest));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_C_BRIGHT_START, "明牌开始", onBrightStartRequest));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_C_BRIGHT, "玩家明牌", onBrightRequest));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_C_DOUBLE_SCORE, "加倍命令", onDoubleScoreRequest));
			
			addMessage(new MessageDef(MDM_GF_GAME, SUB_S_SEND_CARD, "发牌命令", onSendCardNotify));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_S_LAND_SCORE, "叫分命令", onLandScoreNotify));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_S_GAME_START, "游戏开始", onGameStartNotify));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_S_OUT_CARD, "用户出牌", onOutCardNotify));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_S_PASS_CARD, "放弃出牌", onPassCardNotify));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_S_GAME_END, "游戏结束", onGameEndNotify));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_S_BRIGHT_START, "明牌开始", onBrightStartNotify));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_S_BRIGHT_CARD, "玩家明牌", onUserBrightNotify));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_S_DOUBLE_SCORE, "分数加倍", onDoubleScoreNotify));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_S_USER_DOUBLE, "用户加倍", onUserDoubleNotify));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_S_MULTIPLE, "倍数通知", onMultipleNotify));
			addMessage(new MessageDef(MDM_GF_GAME, SUB_S_FLEE_LOSS, "逃跑损失", onFleeLossNotify));
		}
		public function onLandScoreRequest(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			bap.writeByte(packet._parm);
		}
		public function onOutCardRequest(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			var cardData:Array = GameModel.i.outCardData;
			var cardCount:uint = cardData.length;
			bap.writeByte(cardCount);
			
			for(var i:uint = 0; i < cardCount; i++)
				bap.writeByte(cardData[i]);
			
			trace(cardData);
			
		}
		public function onPassCardRequest(packet:MsgPacket):void
		{
			
		}
		public function onTrusteeRequest(packet:MsgPacket):void
		{
			var chairID:int;
			var bap:ByteArrayPlus = packet._data;
			if(packet._type == MsgPacket.MSG_SEND)
			{
				var data:Object = packet._parm;
				chairID = data.chairID;
				if(chairID == -1) return;
				var isTrustee:Boolean = data.isTrustee;
				bap.writeShort(chairID);
				bap.writeBoolean(isTrustee);
			}
			else
			{
				chairID = bap.readShort();
				GameModel.i.gamePlayers[chairID].isTrustee = bap.readBoolean();
			}
		}
		public function onBrightStartRequest(packet:MsgPacket):void
		{
			
		}
		public function onBrightRequest(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			bap.writeByte(packet._parm);
		}
		public function onDoubleScoreRequest(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			bap.writeBoolean(packet._parm);
		}
		
		public function onSendCardNotify(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			GameModel.i.curUser = bap.readShort();
			var cardData:Array = null;
			for(var i:uint = 0; i < Define.GAME_PLAYER; i++)
			{
				cardData = (GameModel.i.gamePlayers[i] as GamePlayer).cardData;
				cardData.length = 0;
				for(var j:uint = 0; j < Define.CARD_MAX_COUNT; j++)
					cardData[j] = bap.readByte();
				cardData.splice(Define.CARD_MAX_COUNT - 3, Define.CARD_MAX_COUNT);
			}
			
			cardData = GameModel.i.backCard;
			cardData.length = 0;
			for(i = 0; i < Define.BACK_CARD_COUNT; i++)
				cardData[i] = bap.readByte();
			
			for(i = 0; i < Define.GAME_PLAYER; i++)
				(GameModel.i.gamePlayers[i] as GamePlayer).isBright = bap.readBoolean();
			
			var tableInfo:TableInfo = GameModel.i.tableInfo;
			for(i = 0; i < Define.GAME_PLAYER; i++)
			{
				var userInfo:UserInfo = UserInfoModel.i.value(tableInfo.userStatus[i].dwUserID);
				if(userInfo)
					GameModel.i.userAccount[i] = userInfo.szAccounts;
			}
			
		}
		public function onLandScoreNotify(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			GameModel.i.preUser = bap.readShort();
			GameModel.i.curUser = bap.readShort();
			GameModel.i.preLandScore = bap.readByte();
			GameModel.i.curLandScore = bap.readByte();
			GameModel.i.preScorePhase = bap.readByte();
			GameModel.i.curScorePhase = bap.readByte();
			readBrightData(bap);
			
//			trace(GameModel.i.preUser,"/",
//				GameModel.i.curUser,"/",
//				GameModel.i.curLandScore,"/",
//				GameModel.i.preLandScore,"/",
//				GameModel.i.preScorePhase,"/",
//				GameModel.i.curScorePhase,"/",
//				GameModel.i.gamePlayers[0].isBright,"/",
//				GameModel.i.gamePlayers[1].isBright,"/",
//				GameModel.i.gamePlayers[2].isBright);
		}
		public function onGameStartNotify(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			GameModel.i.landlordUser = bap.readShort();
			GameModel.i.landlordScore = bap.readByte();
			GameModel.i.curUser = bap.readShort();
			bap.readBoolean();
			readBrightData(bap);
			readBackData(bap);
		}
		public function onOutCardNotify(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			var cardCount:uint = bap.readByte();
			GameModel.i.curUser = bap.readShort();
			GameModel.i.outCardUser = bap.readShort();
			if(cardCount != 0)
			{
				GameModel.i.outCardData.length = 0;
				for(var i:uint = 0; i < cardCount; i++)
				{
					GameModel.i.outCardData.push(bap.readByte());
				}
			}
		}
		public function onPassCardNotify(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			GameModel.i.newTurn = bap.readByte();
			GameModel.i.passUser = bap.readShort();
			GameModel.i.curUser = bap.readShort();
		}
		public function onGameEndNotify(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			GameModel.i.reset();
			GameModel.i.gameTax = bap.readInt();
			GameModel.i.gamePresentTax = bap.readInt();
			GameModel.i.curBrightTime = bap.readShort();
			var i:uint = 0;
			for(i = 0; i < Define.GAME_PLAYER; i++)
			{
				GameModel.i.gameScore[i] = bap.readInt();
			}
			
			var cardCount:Array = [];
			for(i = 0; i < Define.GAME_PLAYER; i++)
			{
				cardCount.push(bap.readByte());
			}
			
			var cardData:Array = [];
			for(i = 0; i < 54; i++)
			{
				cardData.push(bap.readByte());
			}
		}
		public function onBrightStartNotify(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			var brightUser:uint = bap.readShort();
			var isBright:Boolean = bap.readBoolean();
			GameModel.i.gamePlayers[brightUser].isBright = isBright;
			packet._parm = {brightUser:brightUser,isBright:isBright};
		}
		public function onUserBrightNotify(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			GameModel.i.brightUser	  = bap.readShort();
			GameModel.i.preBrightTime = bap.readByte();
			GameModel.i.curBrightTime = bap.readByte();
			GameModel.i.curScorePhase = bap.readByte();
			GameModel.i.gamePlayers[GameModel.i.brightUser].isBright = GameModel.i.preBrightTime != -1;
			
//			trace("onUserBrightNotify:", 
//				"brightUser:",GameModel.i.brightUser, 
//				"preBrightTime:",GameModel.i.preBrightTime, 
//				"curBrightTime:",GameModel.i.curBrightTime, 
//				"curScorePhase:",GameModel.i.curScorePhase);
		}
		public function onDoubleScoreNotify(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			GameModel.i.allowDouble = bap.readBoolean();
			readBackData(bap);
			GameModel.i.landlordUser = bap.readShort();
			GameModel.i.preLandScore = bap.readByte();
		}
		public function onUserDoubleNotify(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			GameModel.i.doubleUser = bap.readShort();
			GameModel.i.isDouble = bap.readBoolean();
			GameModel.i.curLandScore = bap.readByte();
		}
		public function onMultipleNotify(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			GameModel.i.curBrightTime = bap.readShort();
		}
		public function onFleeLossNotify(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			GameModel.i.lostScore = bap.readInt();
			GameModel.i.lostGold = bap.readInt();
		}
		public function readBrightData(bap:ByteArrayPlus):void
		{
			for(var i:uint = 0; i < 3; i++)
				var isBright:Boolean = GameModel.i.gamePlayers[i].isBright = bap.readBoolean();
		}
		public function readBackData(bap:ByteArrayPlus):void
		{
			GameModel.i.backCard.length = 0;
			for(var i:uint = 0; i < 3; i++)
				GameModel.i.backCard.push(bap.readByte());
		}
	}
}